package org.dairy.farms.nandana.core.security.userdetails;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: gduggira
 * Date: Jun 17, 2011
 * Time: 4:50:53 PM
 */
public class NandanaUserDetails implements UserDetails {
    private boolean accountNonExpired;
    private String password;
    private String username;
    private boolean credentialsNonExpired;
    private boolean accountNonLocked;
    private boolean enabled;
    private Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
    private Long personId;
    public NandanaUserDetails(String userName, String password, boolean enabled,
                              boolean accountNonLocked, boolean credentialsNonExpired, boolean accountNonExpired, Long personId){

        this.username = userName;
        this.password = password;
        this.enabled = enabled;
        this.accountNonLocked = accountNonLocked;
        this.credentialsNonExpired = credentialsNonExpired;
        this.accountNonExpired = accountNonExpired;
        this.personId =personId;
    }

    public Collection<GrantedAuthority> getAuthorities() {
        return authorities;
    }

    public String getPassword() {
        return password;
    }

    public String getUsername() {
        return username;
    }

    public boolean isAccountNonExpired() {
        return accountNonExpired;
    }

    public boolean isAccountNonLocked() {
        return accountNonLocked;
    }

    public boolean isCredentialsNonExpired() {
        return credentialsNonExpired;
    }

    public boolean isEnabled() {
        return this.enabled;
    }
    public void addAuthority(NandanaGrantedAuthority nandanaGrantedAuthority){
        authorities.add(nandanaGrantedAuthority);
    }

    public Long getPersonId() {
        return personId;
    }

    public void setPersonId(Long personId) {
        this.personId = personId;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof NandanaUserDetails)) return false;

        NandanaUserDetails that = (NandanaUserDetails) o;

        if (accountNonExpired != that.accountNonExpired) return false;
        if (accountNonLocked != that.accountNonLocked) return false;
        if (credentialsNonExpired != that.credentialsNonExpired) return false;
        if (enabled != that.enabled) return false;
        if (authorities != null ? !authorities.equals(that.authorities) : that.authorities != null) return false;
        if (password != null ? !password.equals(that.password) : that.password != null) return false;
        if (personId != null ? !personId.equals(that.personId) : that.personId != null) return false;
        if (username != null ? !username.equals(that.username) : that.username != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = (accountNonExpired ? 1 : 0);
        result = 31 * result + (password != null ? password.hashCode() : 0);
        result = 31 * result + (username != null ? username.hashCode() : 0);
        result = 31 * result + (credentialsNonExpired ? 1 : 0);
        result = 31 * result + (accountNonLocked ? 1 : 0);
        result = 31 * result + (enabled ? 1 : 0);
        result = 31 * result + (authorities != null ? authorities.hashCode() : 0);
        result = 31 * result + (personId != null ? personId.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "NandanaUserDetails{" +
                "accountNonExpired=" + accountNonExpired +
                ", password='" + password + '\'' +
                ", username='" + username + '\'' +
                ", credentialsNonExpired=" + credentialsNonExpired +
                ", accountNonLocked=" + accountNonLocked +
                ", enabled=" + enabled +
                ", authorities=" + authorities +
                ", personId=" + personId +
                '}';
    }
}
